package com.have.blog.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.have.blog.admin.constant.AdminMessageConstant;
import com.have.blog.admin.dao.RoleResourceMenuDao;
import com.have.blog.admin.domain.dto.AdminRoleDto;
import com.have.blog.admin.redis.AdminLoginCacheService;
import com.have.blog.admin.service.AdminRoleService;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.model.mapper.AdminMapper;
import com.have.blog.model.mapper.AdminRoleMapper;
import com.have.blog.model.models.*;
import com.have.blog.utils.MyBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@SuppressWarnings("all")
public class AdminRoleServiceImpl implements AdminRoleService {
    @Autowired
    private AdminRoleMapper roleMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private AdminLoginCacheService adminCacheService;
    @Autowired
    private RoleResourceMenuDao roleResourceMenuDao;

    @Override
    public List<AdminRoleDto> getList(AdminRoleDto roleDto) {
        PageHelper.startPage(roleDto.getPageNum(), roleDto.getPageSize());
        AdminRoleExample example = new AdminRoleExample();
        AdminRoleExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(roleDto.getKeyword()) && !StringUtils.isEmpty(roleDto.getKeyword().trim())) {
            criteria.andRoleNameLike("%" + roleDto.getKeyword() + "%");
        }
        criteria.andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        List<AdminRole> roleList = roleMapper.selectByExample(example);
        List<AdminRoleDto> resultList = MyBeanUtils.copyListProperties(roleList, AdminRoleDto::new);
        for (AdminRoleDto dto : resultList) {
            List<Integer> resourceUidsByRoleUid = roleResourceMenuDao.getResourceUidsByRoleUid(dto.getUid());
           dto.setResourceMenuUids(resourceUidsByRoleUid);
        }
        return resultList;
    }

    @Transactional
    @Override
    public Wrapper add(AdminRoleDto roleDto) {
        AdminRoleExample roleExample = new AdminRoleExample();
        roleExample.createCriteria()
                .andRoleNameEqualTo(roleDto.getRoleName())
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        List<AdminRole> AdminRoles = roleMapper.selectByExample(roleExample);
        if (!CollectionUtils.isEmpty(AdminRoles)) {
            return Wrapper.failed(AdminMessageConstant.ROLE_NAME_EXIST);
        }
        AdminRole role = new AdminRole();
        MyBeanUtils.copyProperties(roleDto, role);
        roleMapper.insertSelective(role);
        int count = roleResourceMenuDao.insertList(roleDto.getResourceMenuUids(), role.getUid());
        if (count > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.INSERT_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.INSERT_FAIL);
    }

    @Transactional
    @Override
    public Wrapper edit(AdminRoleDto roleDto) {
        String roleName = roleDto.getRoleName();
        AdminRoleExample roleExample = new AdminRoleExample();
        roleExample.createCriteria()
                .andRoleNameEqualTo(roleName);
        List<AdminRole> AdminRoles = roleMapper.selectByExample(roleExample);
        if (!CollectionUtils.isEmpty(AdminRoles) && !AdminRoles.get(0).getUid().equals(roleDto.getUid())) {
            return Wrapper.failed(AdminMessageConstant.ROLE_NAME_EXIST);
        }
        AdminRole adminRole = MyBeanUtils.copyProperties(roleDto, AdminRole::new);
        roleResourceMenuDao.deleteByRoleUid(adminRole.getUid());
        List<Integer> resourceMenuUids = roleDto.getResourceMenuUids();
        System.out.println(resourceMenuUids);
        roleResourceMenuDao.insertList(resourceMenuUids, adminRole.getUid());
        int update = roleMapper.updateByPrimaryKeySelective(adminRole);
        // 更新权限后需要删除缓存中的权限
        if (update > 0) {
            adminCacheService.deleteAllResourceList();
            return Wrapper.successWithMessage(AdminMessageConstant.UPDATE_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.UPDATE_FAIL);
    }

    @Override
    public Wrapper delete(AdminRoleDto roleDto) {
        Integer uid = roleDto.getUid();
        AdminExample adminExample = new AdminExample();
        adminExample.createCriteria()
                .andRoleUidEqualTo(uid);
        long count = adminMapper.countByExample(adminExample);
        if (count > 0) {
            return Wrapper.failed(AdminMessageConstant.ADMIN_UNDER_THIS_ROLE);
        }
        AdminRole role = new AdminRole();
        role.setUid(uid);
        role.setStatus(BlogStatus.DISABLE.getStatus());
        roleResourceMenuDao.deleteByRoleUid(roleDto.getUid());
        int delete = roleMapper.updateByPrimaryKeySelective(role);
        // 更新权限后需要删除缓存中的权限
        if (delete > 0) {
            adminCacheService.deleteAllResourceList();
            return Wrapper.successWithMessage(AdminMessageConstant.DELETE_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.DELETE_FAIL);
    }

    @Override
    public AdminRoleDto getAdminRoleByUid(Integer uid) {
        AdminRole role = roleMapper.selectByPrimaryKey(uid);
        AdminRoleDto adminRoleDto = MyBeanUtils.copyProperties(role, AdminRoleDto::new);
        return adminRoleDto;
    }
}
